package com.maxvision.mybatis.controller;

import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;

/**
 * 类说明
 *
 * @author HuangRongFu
 * @date 2023/3/21
 */

@ControllerAdvice
public class BaseController {
    
    @ResponseBody
    @ExceptionHandler(MultipartException.class)
    public ResponseEntity fileUploadExceptionHandler(MultipartException exception) {

        Throwable rootCause = exception.getRootCause();
        if (rootCause instanceof  IllegalStateException 
                || rootCause instanceof MaxUploadSizeExceededException 
                || rootCause instanceof FileSizeLimitExceededException){
            
            return new ResponseEntity("文件过大", HttpStatus.INTERNAL_SERVER_ERROR);
        }
        
        return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
